﻿using adb;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using System.Linq;
using Z.EntityFramework.Plus;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using adb.excel;
using System.IO;
using Microsoft.Net.Http.Headers;
using System;
using PowerStation.AppCode;
using Microsoft.AspNetCore.Authorization;
using System.Text;

namespace PowerStation.Controllers
{
    /// <summary>
    /// 监控管理
    /// </summary>
    public class LiveController : LiveBaseApi<LiveController>
    {


        /// <summary>
        /// 监控管理
        /// </summary>
        /// <param name="content"></param>
        public LiveController(IHttpContextAccessor content) : base(content)
        {

        }


        /// <summary>
        /// 获取m3u8,ts文件
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        [Route("live/{DeivceSN}/{FileName}.m3u8")]
        [Route("live/{DeivceSN}/{FileName}.ts")]
        public FileStreamResult Index([FromRoute] string DeivceSN, [FromRoute] string FileName)
        {
            try
            {
                if (Request.Path.Value.Contains(".m3u8"))
                {
                    FileInfo info = new FileInfo($@"V:\{DeivceSN}\{FileName}.m3u8");
                    if (info.Exists)
                    {
                        DeviceStream file = new DeviceStream($@"V:\{DeivceSN}\{FileName}.m3u8");
                        return new FileStreamResult(file, "application/vnd.apple.mpegurl")
                        {
                            FileDownloadName = $"{FileName}.m3u8"
                        };
                    }
                }
                if (Request.Path.Value.Contains(".ts"))
                {
                    FileInfo info = new FileInfo($@"V:\{DeivceSN}\{FileName}.ts");
                    if (info.Exists)
                    {
                        DeviceStream file = new DeviceStream($@"V:\{DeivceSN}\{FileName}.ts");
                        return new FileStreamResult(file, "video/mp2t")
                        {
                            FileDownloadName = $"{FileName}.ts"
                        };
                    }
                }
                Response.StatusCode = 404;
                Response.Body.Write(Encoding.UTF8.GetBytes("Not Found File"));
                return null;
            }
            catch (Exception ex)
            {
                Response.StatusCode = 404;
                Response.Body.WriteAsync(Encoding.UTF8.GetBytes("Not found file")); 
                return null;
            }
        }


        /// <summary>
        /// 获取监控列表
        /// </summary>
        /// <param name="powerStationId">电站ID</param>
        /// <returns></returns>
        [Route("api/[controller]/[action]")]
        [HttpGet]   
        public async Task<ResultApi<List<vVideoList>>> VideoList(decimal powerStationId)
        {
            await this.IsCheckPowerStation(powerStationId);

            IQueryable<vVideoList> q = this._db.vVideoList.Where(x => x.devicePowerStation_powerStationId_fk == powerStationId).OrderBy(x => x.addTime);
            q = q.OrderBy(x => x.dataOrderBy);
            return ResultApi.ok<List<vVideoList>>(await q.ToListAsync(),0);
        }

    }
}
